package com.ruoyi.web.controller.business;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DictUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.system.domain.TbDept;
import com.ruoyi.system.domain.dto.DtoUser;
import com.ruoyi.system.domain.vo.VoUser;
import com.ruoyi.system.service.ISysDictDataService;
import com.ruoyi.system.service.ITbDeptService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.TbVisitorUser;
import com.ruoyi.system.service.ITbVisitorUserService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 访客预约信息Controller
 * 
 * @author ruoyi
 * @date 2024-04-10
 */
@RestController
@RequestMapping("/business/user")
public class TbVisitorUserController extends BaseController
{
    @Autowired
    private ITbVisitorUserService tbVisitorUserService;

    @Autowired
    private ITbDeptService tbDeptService;

    @Autowired
    private ISysDictDataService dictDataService;

    /**
     * 查询访客预约信息列表
     */
    @GetMapping("/list")
    public TableDataInfo list(DtoUser tbVisitorUser)
    {
        startPage();
        List<TbVisitorUser> list = tbVisitorUserService.selectTbVisitorUserList(tbVisitorUser);

        if (list != null && list.size() > 0) {

            List<VoUser> voList = new ArrayList<VoUser>();

            for (TbVisitorUser item : list) {
                VoUser vo = new VoUser();
                BeanUtils.copyProperties(item, vo);

                if (StringUtils.isNotBlank(item.getCompanyTo())) {
                    vo.setCompanyToName(DictUtils.getDictLabel("visiting_unit", item.getCompanyTo()));
                }

                if (StringUtils.isNotBlank(item.getVisitingBase())) {
                    vo.setVisitingBaseName(DictUtils.getDictLabel("visiting_base", item.getVisitingBase()));
                }

                String customersType = item.getCustomersType();
                String visitorType = item.getVisitorType();

                vo.setCustomersTypeName(DictUtils.getDictLabel("visitor_user", customersType));
                vo.setVisitorTypeName(DictUtils.getDictLabel(customersType, visitorType));

                voList.add(vo);
            }
            TableDataInfo rspData = getDataTable(voList);
            rspData.setTotal(tbVisitorUserService.selectTbVisitorUserCnt(tbVisitorUser));
            return rspData;
        }
        return getDataTable(list);
    }

    /**
     * 导出访客预约信息列表
     */
    @PostMapping("/export")
    public void export(HttpServletResponse response, DtoUser tbVisitorUser)
    {
        List<TbVisitorUser> list = tbVisitorUserService.selectTbVisitorUserList(tbVisitorUser);
        ExcelUtil<TbVisitorUser> util = new ExcelUtil<TbVisitorUser>(TbVisitorUser.class);
        util.exportExcel(response, list, "访客预约信息数据");
    }

    /**
     * 获取访客预约信息详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(tbVisitorUserService.selectTbVisitorUserById(id));
    }

    /**
     * 新增访客预约信息
     */
    @PostMapping
    public AjaxResult add(@RequestBody TbVisitorUser tbVisitorUser)
    {
        return toAjax(tbVisitorUserService.insertTbVisitorUser(tbVisitorUser));
    }

    /**
     * 修改访客预约信息
     */
    @PutMapping
    public AjaxResult edit(@RequestBody TbVisitorUser tbVisitorUser)
    {
        return toAjax(tbVisitorUserService.updateTbVisitorUser(tbVisitorUser));
    }

    /**
     * 删除访客预约信息
     */
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(tbVisitorUserService.deleteTbVisitorUserByIds(ids));
    }
}
